package com.mingmen.canting.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.CaiDingWanModel;
import com.mingmen.canting.model.LoginReturnBean;
import com.mingmen.canting.model.PeiSongDetailModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Contact联系人适配器
 *
 * @author nanchen
 * @fileName WaveSideBarView
 * @packageName com.nanchen.wavesidebarview
 * @date 2016/12/27  15:33
 * @github https://github.com/nanchen2251
 */

public class CaiGouWanListAdapter extends RecyclerView.Adapter<CaiGouWanListAdapter.ContactsViewHolder> {

    private List<CaiDingWanModel.DataBean.DetailsBean> dataList;
    //private List<String> dataList = new ArrayList<>();
    private static final String TAG = "ContactsAdapter";
    private OnitemClick onitemClick;   //定义点击事件接口
    Context context;

    public CaiGouWanListAdapter(Context context, List<CaiDingWanModel.DataBean.DetailsBean> dataList) {
        this.context = context;
        this.dataList = dataList;
    }

    //定义设置点击事件监听的方法
    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    int flag = 0;

    public void setDataList(List<CaiDingWanModel.DataBean.DetailsBean> dataList) {
        this.dataList = dataList;

        notifyDataSetChanged();

    }

    public void setFlag(int flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }

    View view;

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(parent.getContext());


        view = inflater.inflate(R.layout.layaout_item_caigouwan, null);

        return new ContactsViewHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
//        return super.getItemViewType(position);
        if (flag == 0) {
            return 0;
        } else if (flag == 1) {
            return 1;
        } else {
            return super.getItemViewType(position);
        }
    }


    @Override
    public void onBindViewHolder(ContactsViewHolder holder, int position) {

        holder.tvName.setText(dataList.get(position).getCommodityName());
        holder.confirmAmount.setText(dataList.get(position).getConfirmAmount() + "");
        holder.confirmPrice.setText("￥" + dataList.get(position).getConfirmPrice() + "");
        Glide.with(context).load(AppConfig.IMGURL + dataList.get(position).getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(context, 5)).into(holder.ivAvatar);
//        holder.text_Spec.setText("规格:" + dataList.get(position).getSpecs());
        holder.text_Spec.setText("数量:" + dataList.get(position).getArriveAmount());
        if (flag == 0) {
            if (position == 0) {
                holder.layout_more.setVisibility(View.VISIBLE);
                holder.layout_item.setVisibility(View.VISIBLE);
            } else {
                holder.layout_more.setVisibility(View.GONE);
                holder.layout_item.setVisibility(View.GONE);
            }
        } else if (flag == 1) {
            holder.layout_more.setVisibility(View.GONE);
            holder.layout_item.setVisibility(View.VISIBLE);
//            holder.layout_mshouqi.setVisibility(View.VISIBLE);
            if (dataList.size() - 1 == position) {
                holder.layout_mshouqi.setVisibility(View.VISIBLE);
            } else {
                holder.layout_mshouqi.setVisibility(View.GONE);
            }
        }
        holder.layout_more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, position);
            }
        });
        holder.layout_mshouqi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onitemClick.onItemClick(v, position);
            }
        });

        if (dataList.size() == 1) {
            holder.layout_more.setVisibility(View.GONE);
            holder.layout_mshouqi.setVisibility(View.GONE);
        }
    }


    @Override
    public int getItemCount() {
    /*    if (flag == 0) {
            return 1;
        } else {
            return dataList.size();
        }*/
        return dataList.size();
     /*   Log.d("DataList", dataList.size() + "");
        return dataList.size();*/

    }

    class ContactsViewHolder extends RecyclerView.ViewHolder {
        TextView tvName;
        LinearLayout layout_item;
        LinearLayout layout_more;
        LinearLayout layout_mshouqi;
        TextView confirmAmount;
        TextView confirmPrice;
        ImageView ivAvatar;

        TextView text_Spec;

        ContactsViewHolder(View itemView) {
            super(itemView);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            layout_item = itemView.findViewById(R.id.layout_item);
            layout_more = itemView.findViewById(R.id.layout_more);
            layout_mshouqi = itemView.findViewById(R.id.layout_mshouqi);
            confirmAmount = itemView.findViewById(R.id.confirmAmount);
            confirmPrice = itemView.findViewById(R.id.confirmAmount);
            ivAvatar = itemView.findViewById(R.id.ivAvatar);
            text_Spec = itemView.findViewById(R.id.text_Spec);
        }
    }

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int position);
    }
}
